package java_ex.view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import java_ex.controller.PrimeNumber;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class View extends JFrame {

    private static final long serialVersionUID = -6891030593703055332L;
    private JLabel mouseoutput[] = new JLabel[5];
    private String[] mouselabeltext = new String[] { "Mouse Click:",
            "Mouse Enter:", "Mouse Exit", "Mouse Pressed:", "Mouse Relesed:" };
    // 0 = main, 1 = top, 2 = left, 3 = right, 4 = center, 5 = bottom,
    // 6 = leftsub
    private JPanel[] panels = new JPanel[7];
    private JTextField input = new JTextField(25);
    private JTextArea result = new JTextArea("Gefundene Primzahlen:\n");
    private JLabel progess = new JLabel();

    public View(String name) {
        super(name);
        initLayout();
        initSetController();

        this.add(panels[0]);
        this.setName(name);
        this.setBounds(50, 50, 640, 480);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    private void initLayout() {
        for (int i = 0; i < panels.length; i++) {
            panels[i] = new JPanel();
        }

        panels[0].setLayout(new BorderLayout());
        panels[2].setLayout(new GridLayout(2, 1));
        panels[4].setLayout(new BorderLayout());
        panels[6].setLayout(new GridLayout(5, 2));
        JButton calc = new JButton("Finde");

        // Top
        panels[1].add(new JLabel("Finde alle Primzahlen bis:"));
        panels[1].add(input);
        panels[1].add(calc);
        calc.addActionListener(new GuiController());

        // Left
        panels[2].add(new JLabel("Maus Events"));
        for (int i = 0; i < mouseoutput.length; i++) {
            JLabel l = new JLabel("0");
            panels[6].add(new JLabel(mouselabeltext[i]));
            panels[6].add(l);
            mouseoutput[i] = l;
        }
        panels[2].add(panels[6]);

        // Right
        panels[3].add(new JLabel("Hallo Welt"));

        // Center
        result.setLineWrap(true);
        JScrollPane s = new JScrollPane(result);
        panels[4].add(s);

        // Bottom
        panels[5].add(new JLabel("Aktuelle Zahl:"));
        panels[5].add(progess);

        panels[0].add(panels[1], BorderLayout.NORTH);
        panels[0].add(panels[2], BorderLayout.WEST);
        panels[0].add(panels[3], BorderLayout.EAST);
        panels[0].add(panels[4], BorderLayout.CENTER);
        panels[0].add(panels[5], BorderLayout.SOUTH);
    }

    private void initSetController() {
        GuiController mouse = new GuiController();
        for (JPanel p : panels) {
            p.addMouseListener(mouse);
        }
        this.addMouseListener(mouse);

    }

    class GuiController implements MouseListener, ActionListener {

        @Override
        public void mouseClicked(MouseEvent e) {
            Integer i = Integer.parseInt(mouseoutput[0].getText());
            mouseoutput[0].setText(String.valueOf(++i));
        }

        @Override
        public void mouseEntered(MouseEvent e) {
            Integer i = Integer.parseInt(mouseoutput[1].getText());
            mouseoutput[1].setText(String.valueOf(++i));
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Integer i = Integer.parseInt(mouseoutput[2].getText());
            mouseoutput[2].setText(String.valueOf(++i));
        }

        @Override
        public void mousePressed(MouseEvent e) {
            Integer i = Integer.parseInt(mouseoutput[3].getText());
            mouseoutput[3].setText(String.valueOf(++i));
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            Integer i = Integer.parseInt(mouseoutput[4].getText());
            mouseoutput[4].setText(String.valueOf(++i));
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            result.setText("Gefundene Primzahlen:\n");
            PrimeNumber task = new PrimeNumber(Integer.parseInt(input.getText()), result);
            task.addPropertyChangeListener(new PropertyChangeListener() {
                
                @Override
                public void propertyChange(PropertyChangeEvent arg0) {
                    if ("nextnumber".equals(arg0.getPropertyName())) {                        
                        progess.setText(String.valueOf(arg0.getNewValue()));
                    }else if("primefound".equals(arg0.getPropertyName())){
                        result.append(arg0.getNewValue() + " ");
                    }
                }
            });
            task.execute();
        }

    }

}
